package com.ruoyi.web.controller.cat;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cat.domain.AdMedium;
import com.ruoyi.cat.service.IAdMediumService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 媒体广告管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/system/medium")
public class AdMediumController extends BaseController
{
    @Autowired
    private IAdMediumService adMediumService;

    /**
     * 查询媒体广告管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:medium:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdMedium adMedium)
    {
        startPage();
        List<AdMedium> list = adMediumService.selectAdMediumList(adMedium);
        return getDataTable(list);
    }

    /**
     * 导出媒体广告管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:medium:export')")
    @Log(title = "媒体广告管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdMedium adMedium)
    {
        List<AdMedium> list = adMediumService.selectAdMediumList(adMedium);
        ExcelUtil<AdMedium> util = new ExcelUtil<AdMedium>(AdMedium.class);
        util.exportExcel(response, list, "媒体广告管理数据");
    }

    /**
     * 获取媒体广告管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:medium:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(adMediumService.selectAdMediumById(id));
    }

    /**
     * 新增媒体广告管理
     */
    @PreAuthorize("@ss.hasPermi('system:medium:add')")
    @Log(title = "媒体广告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdMedium adMedium)
    {
        return toAjax(adMediumService.insertAdMedium(adMedium));
    }

    /**
     * 修改媒体广告管理
     */
    @PreAuthorize("@ss.hasPermi('system:medium:edit')")
    @Log(title = "媒体广告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdMedium adMedium)
    {
        return toAjax(adMediumService.updateAdMedium(adMedium));
    }

    /**
     * 删除媒体广告管理
     */
    @PreAuthorize("@ss.hasPermi('system:medium:remove')")
    @Log(title = "媒体广告管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adMediumService.deleteAdMediumByIds(ids));
    }

    /**
     * 修改弹出时间
     * @param adMedium
     * @return
     */
    @PostMapping("modifyPopUp")
    public AjaxResult modifyPopUp(@RequestBody AdMedium adMedium)
    {
        return success(adMediumService.updateAdMedium(adMedium));
    }

    /**
     * 修改增量
     * @param adMedium
     * @return
     */
    @PostMapping("updateSpike")
    public AjaxResult updateSpike(@RequestBody AdMedium adMedium)
    {
        adMedium.setState("0");
        return success(adMediumService.updateAdMedium(adMedium));
    }

    @PostMapping("updateState")
    public AjaxResult updagteState(@RequestBody AdMedium adMedium)
    {
        AdMedium adMedium1 =   adMediumService.selectAdMediumById(adMedium.getId());
        if (adMedium1.getState().equals("0")) {
            adMedium1.setState("3");
            adMediumService.updateAdMedium(adMedium1);
        }else {
            adMedium1.setState("0");
            adMediumService.updateAdMedium(adMedium1);
        }
        return success();
    }
}
